/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *PmResourceApplyLineController
 * 资源申请明细
 *
 * 修改纪录
 * 2020-05-13 版本：1.0 shenyouyi 创建。
 * @version 版本：1.0
 * @author 作者：shenyouyi
 * 创建日期：2020-05-13
 */

package com.topscomm.pm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pm.controller.auto.PmResourceApplyLineControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: shenyouyi
 * @date: 2020-05-13
 * @description:资源申请明细Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/resourceApplyLine")
public class PmResourceApplyLineController extends PmResourceApplyLineControllerAuto
		implements IPmResourceApplyLineController {
	/**
	 * @author: shenyouyi
	 * @date: 2020-05-13
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("parentid")) {
			String value = paraMap.get("parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and resourceapplyid = '{0}' ", value);
		}
		if (paraMap.containsKey("projectid")) {
			String value = paraMap.get("projectid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectid = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-13
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		super.refList(paraMap);

		// 查询此项目下已审核的资源申请单下的资源明细
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		Query query = new Query(paraMap);
		StringBufferProxy sqlForRefList = new StringBufferProxy();
		sqlForRefList.appendSingle(
				"pmresourceapplyline.enabled=1 and EXISTS (select id from pmresourceapply where pmresourceapplyline.resourceapplyid = pmresourceapply.id and pmresourceapply.state=9 and pmresourceapply.projectid='{0}' and pmresourceapply.enabled=1)",
				ConvertUtil.convertToString(paraMap.get("projectid")));
		int totalCount = this.service.getCount(sqlForRefList.toString());

		List<Map<String, Object>> queryMapByWhere = this.service.queryMapForPage(sqlForRefList.toString(),query.getCurrentPage(), query.getPageSize(),
				query.getSidx(), query.getSord());
		this.service.setDispFields(queryMapByWhere);
		Page page = new Page(queryMapByWhere, totalCount, query.getPageSize(), query.getCurrentPage());
		return ResponseResult.ok(page);
	}


}
